package com.sportschain.app.core.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.sportschain.app.R;

/**
 * @author LYZ 2019.03.23
 */
public class CircleView extends View {

    private float mRadius;
    private Rect mBounds = new Rect();

    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CircleView, defStyleAttr, 0);

        if (attributes != null) {
            int color = attributes.getColor(R.styleable.CircleView_color, Color.WHITE);
            mPaint.setColor(color);
        }


    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mRadius = Math.min(getMeasuredHeight(), getMeasuredWidth()) / 2F;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.getClipBounds(mBounds);
        canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), mRadius, mPaint);
    }

    public void setColor(@ColorInt int color) {
        mPaint.setColor(color);
        invalidate();
    }
}
